﻿//************************************************************************************************************
//文件名称(File Name)：AuthorityMana.cs
//功能描述(Description)：权限管理
//数据表(Tables)：
//作者(Author)：
//日期(Create Date)：20130222
//参考文档(Reference)(可选)：
//引用(Using) (可选)﹕
//************************************************************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DbAdapter.sqldbAdapter;

namespace AnalyseCrmMana
{
    public class AuthorityMana
    {
        public int authorityno
        {
            get;
            set;
        }
        public string url
        {
            get;
            set;
        }
        public string authorityname
        {
            get;
            set;
        }
        public int nodeid
        {
            get;
            set;
        }
        public int pnodeid
        {
            get;
            set;
        }

        /// <summary>
        /// 新增权限
        /// </summary>
        /// <param name="author"></param>
        /// <returns></returns>
        public int AndNewAuthority(AuthorityMana author)
        {
            string sql = "Insert into Tbt_Authority(AUTHORITYNAME,URL,NODEID,PNODEID) "
                + "Values('" + author.authorityname + "','" + author.url + "',"
                + author.nodeid.ToString() + "," + author.pnodeid.ToString() + ")";
            ExecuteSql esql = new ExecuteSql();
            return esql.Execute_Sql(sql);
        }

        /// <summary>
        /// 删除权限
        /// </summary>
        /// <param name="authorno"></param>
        /// <returns></returns>
        public int DeleteAuthority(int authorno)
        {
            string sql = "Delete from Tbt_Authority where AUTHORITYNO = " + authorno.ToString();
            ExecuteSql esql = new ExecuteSql();
            return esql.Execute_Sql(sql);
        }

        /// <summary>
        /// 修改权限
        /// </summary>
        /// <param name="author"></param>
        /// <returns></returns>
        public int UpdateAuthority(AuthorityMana author)
        {
            string sql = "Update Tbt_Authority set URL = '" + author.url + "',AUTHORITYNAME = '"
                + author.authorityname + "',NODEID=" + author.nodeid + ",PNODEID=" + author.pnodeid
                + " where AUTHORITYNO = " + author.authorityno.ToString();
            ExecuteSql esql = new ExecuteSql();
            return esql.Execute_Sql(sql);
        }

        public IList<AuthorityMana> getAllauthoritys()
        {
            string sql = "select AUTHORITYNO,AUTHORITYNAME,URL,NODEID,PNODEID from Tbt_Authority";
            sqldb db = new sqldb();
            DataSet ds = db.ExecuteDataset(sqlconn.GetConnStr(), CommandType.Text, sql);
            IList<AuthorityMana> authlist = null;
            if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
            {
                authlist = new List<AuthorityMana>();
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    AuthorityMana authmana = new AuthorityMana();
                    authmana.authorityno = Convert.ToInt32(dr["AUTHORITYNO"]);
                    authmana.authorityname = dr["AUTHORITYNAME"].ToString();
                    authmana.url = dr["URL"].ToString();
                    authmana.nodeid = Convert.ToInt32(dr["NODEID"]);
                    authmana.pnodeid = Convert.ToInt32(dr["PNODEID"]);
                    authlist.Add(authmana);
                }
            }
            return authlist;
        }

        /// <summary>
        /// 权限数量
        /// </summary>
        /// <returns></returns>
        public int getAllauthoritysCount()
        {
            string sql = "select count(1) from Tbt_Authority";
            sqldb db = new sqldb();
            object rowcount = db.ExecuteScalar(sqlconn.GetConnStr(), CommandType.Text, sql);
            return Convert.ToInt32(rowcount);
        }
    }

    public class SelectAuthority
    {
        public int ischecked
        {
            get;
            set;
        }
        public int authorityno
        {
            get;
            set;
        }
        public string authorityname
        {
            get;
            set;
        }
        /// <summary>
        /// 根据角色编号获取选择的权限
        /// </summary>
        /// <param name="roleno"></param>
        /// <returns></returns>
        public IList<SelectAuthority> getAllauthoritysByrole(string roleno)
        {
            string sql = "select CASE WHEN B.AUTHORITYNO is null then 0 else 1 end ISCHECKED,"
                       + "A.AUTHORITYNO,A.AUTHORITYNAME from Tbt_Authority A LEFT JOIN "
                       + "(SELECT * FROM Tbt_AuthorityAndRole WHERE ROLENO = '"+roleno+"') B "
                       + "ON A.AUTHORITYNO = B.AUTHORITYNO";
            sqldb db = new sqldb();
            DataSet ds = db.ExecuteDataset(sqlconn.GetConnStr(), CommandType.Text, sql);
            IList<SelectAuthority> authlist = null;
            if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
            {
                authlist = new List<SelectAuthority>();
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    SelectAuthority selauth = new SelectAuthority();
                    selauth.ischecked = Convert.ToInt32(dr["ISCHECKED"]);
                    selauth.authorityno = Convert.ToInt32(dr["AUTHORITYNO"]);
                    selauth.authorityname = dr["AUTHORITYNAME"].ToString();
                    authlist.Add(selauth);
                }
            }
            return authlist;
        }
    }
}
